package com.example.flowabledemo.config.db;

import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

/**
 * @description 废弃多数据源，但是保留该配置
 * 2020年12月5日21:48:47
 */
//@Configuration
@Deprecated
public class MyBatisConfig {

  @Value("${mybatis.mapper-locations}")
  private String materLocation;

  /**
   * mybatis配置主数据源
   *
   * @param masterDataSource
   * @return
   * @throws Exception
   */
  @Bean(name = "masterSqlSessionFactory")
  @Primary
  public SqlSessionFactory masterSqlSessionFactory(@Qualifier("dataSource") DataSource masterDataSource)
      throws Exception {
    final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(masterDataSource);
    sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
        .getResources(materLocation));
    return sessionFactory.getObject();
  }

  /**
   * mybatis配置flowable据源
   *
   * @param masterDataSource
   * @return
   * @throws Exception
   */
  @Bean(name = "flowableSqlSessionFactory")
  public SqlSessionFactory flowableSqlSessionFactory(@Qualifier("flowableDataSource") DataSource masterDataSource)
      throws Exception {
    final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
    sessionFactory.setDataSource(masterDataSource);
    sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
        .getResources(materLocation));
    return sessionFactory.getObject();
  }

}
